package com.lxl.settings.controller;


import com.lxl.commons.contants.Contants;
import com.lxl.settings.entity.User;
import com.lxl.settings.service.StaffService;
import com.lxl.settings.service.UserService;
import com.lxl.settings.service.impl.StaffServiceImpl;
import com.lxl.settings.service.impl.UserServiceImpl;
import com.lxl.workbench.entity.Administrator;
import com.lxl.workbench.entity.CategoryStaff;
import com.lxl.workbench.entity.CommonStaff;
import com.lxl.workbench.entity.Curator;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 先是访问这个，看七天免登录的cookie在不在
 * @author 北上寻海
 */
@WebServlet("/welcome")
public class WelcomeServlet extends HttpServlet {
    @Override
    public void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        /**
         * userService和staffService对象
         */
        UserService userService = new UserServiceImpl();
        StaffService staffService = new StaffServiceImpl();

        //七天免登录验证，看之前是否有存到cookie进去，拿出来连数据库验证
        Cookie[] cookies = request.getCookies();
        //置空判断，不然空值的话会出现空指针异常
        if(null!=cookies) {
            String username = null;
            String password = null;
            List<User> list =new ArrayList<>();
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                if (Contants.USERNAME_COOKIE.equals(name)) {
                    username = cookie.getValue();
                } else if (Contants.PASSWORD_COOKIE.equals(name)) {
                    password = cookie.getValue();
                }
            }

            try {
                list = userService.queryUser(username, password);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //判断是否有
            if (list != null && !list.isEmpty()) {
                String permissions = null;
                User user = list.get(0);
                String id = user.getId();
                try {
                    List<CommonStaff> commonStaffList = staffService.queryCommonStaffById(id);
                    if(commonStaffList==null||commonStaffList.isEmpty()){
                        List<Curator> curatorList = staffService.queryCuratorById(id);
                        if(curatorList==null||curatorList.isEmpty()){
                            List<CategoryStaff> categoryStaffList = staffService.queryCategoryStaffById(id);
                            if(categoryStaffList==null||categoryStaffList.isEmpty()){
                                List<Administrator> administratorList = staffService.queryAdministratorById(id);
                                permissions = administratorList.get(0).getPermissions();
                            }else {
                                permissions=categoryStaffList.get(0).getPermissions();
                                String manageType = categoryStaffList.get(0).getManageType();
                                request.setAttribute("manageType",manageType);
                            }
                        }else {
                            permissions=curatorList.get(0).getPermissions();
                        }
                    }else {
                        permissions = commonStaffList.get(0).getPermissions();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                //登录成功存user
                request.getSession().setAttribute(Contants.SESSION_USER,user);

                if(Contants.CURATOR_PERMISSIONS.equals(permissions)){
                    //重定向到馆长工作页面
                    response.sendRedirect("/curatorWork.jsp");

                }else if(Contants.COMMON_STAFF_PERMISSIONS.equals(permissions)){
                    //重定向到普通员工工作页面
                    response.sendRedirect("/commonStaffWork.jsp");

                }else if(Contants.CATEGORY_STAFF_PERMISSIONS.equals(permissions)){
                    //重定向到书目管理员工作页面
                    request.getRequestDispatcher("/categoryStaffWork.jsp").forward(request,response);

                }else if(Contants.ADMINISTRATOR.equals(permissions)){
                    //重定向到书目管理员工作页面
                    response.sendRedirect("/administratorWork.jsp");
                }
            }
        }else {
            response.sendRedirect(request.getContextPath()+"/index.jsp");
        }
    }
}
